#include <bits/stdc++.h>
#define MAX 1000001
#define INF 2e9
#define MOD 1000000007
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define ins insert
#define ff first
#define ss second
#define gett(x,m) get<m>(x)
#define all(a) a.begin(),a.end()
#define lb(a,b) lower_bound(all(a),b)
#define ub(a,b) upper_bound(all(a),b)
#define sortv(a) sort(all(a))
#define sorta(a,sz) sort(a,a+sz)
#define inputar(a,b){\
for(int i=0;i<b;i++){\
cin >> a[i];\
}\
}
#define inputvec(a,b){\
for(int i=0;i<b;i++){\
ll num;\
cin >> num;\
a.pb(num);\
}\
}
#define outputar(a,b){\
for(int i=0;i<b;i++){\
cout << a[i] << " ";\
}\
cout << "\n";\
}
#define outputvec(a){\
for(auto x:a){\
cout << x << " ";\
}\
cout << "\n";\
}
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef tuple<ll,ll,ll> tll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
inline void USACO(string filename){
freopen((filename+".in").c_str(),"r",stdin);
freopen((filename+".out").c_str(),"w",stdout);
}
ll n,q,t=1,m,n2,m2,k,cnt=0,a[MAX],b[MAX],d[MAX],x,y,z,x2,y2,z2,res1=0,cnt1,cnt2,cnt3;
ll c[501][501];
ll fact[MAX];
ll inv_fact[MAX];
//char c;
string str[MAX];
string s1,s2;
vector<tuple<ll,ll,ll>> v;
vector<pll> v1,v2,v3,v4;
const int mod = 998244353;
ll modmul(ll x,ll y,ll md){
if(y==1){
return x;
}
if(y%2){
return (x+modmul(x,y-1,md))%md;
}
else{
return (modmul((x+x)%md,y/2,md))%md;
}
}
ll powmod(ll x,ll y,ll md){
x%=md;
if(x==0){
return 0;
}
ll res=1;
while(y){
if(y%2==1){
//res=modmul(res,x,md);
res*=x;
res%=MOD;
y--;
}
else{
//x=modmul(x,x,md);
x*=x;
x%=MOD;
y/=2;
}
}
return res;
}
ll pow2(ll x,ll y){
if(x==0){
return 0;
}
ll res=1;
while(y>0){
if(y%2==1){
res*=x;
}
x*=x;
y/=2;
}
return res;
}
ll inv(ll n,ll md){
return powmod(n,md-2,md);
}
ll nCkm(ll n,ll k,ll md){
if(n-k<0){
return 0;
}
return fact[n]*inv_fact[k]%md*inv_fact[n-k]%md;
}
ll nCk(ll x,ll y){
if(x<y){
return 0;
}
ll res=1;
if(y>x-y){
for(int i=y+1;i<=x;i++){
res*=i;
}
for(int i=2;i<=x-y;i++){
res/=i;
}
}
else{
for(int i=x-y+1;i<=x;i++){
res*=i;
}
for(int i=2;i<=y;i++){
res/=i;
}
}
return res;
}
ll countbits(ll x){
ll cnt=0;
while(x){
cnt+=x&1;
x>>=1;
}
return cnt;
}
ll gcd(ll x,ll y){
if(y==0){
return x;
}
return gcd(y,x%y);
}
ll lcm(ll x,ll y){
return x*y/gcd(x,y);
}
bool alpha(char c1,char c2){
ll h1,h2;
if(c1>='a'){
h1=c1-'a';
}
else{
h1=c1-'A';
}
if(c2>='a'){
h2=c2-'a';
}
else{
h2=c2-'A';
}
if(h1==h2){
return c1<c2;
}
else{
return h1<h2;
}
}
ll dx[8]={1,2,2,1,-1,-2,-2,-1};
ll dy[8]={-2,-1,1,2,2,1,-1,-2};
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
struct segtree{
int size;
vector<ll> sums;
void build(vector<int> &a,int x,int lx,int rx){
if(rx-lx==1){
if(lx<(int)a.size()){
sums[x]=a[lx];
}
return;
}
int m=(lx+rx)/2;
build(a,2*x+1,lx,m);
build(a,2*x+2,m,rx);
sums[x]=sums[2*x+1]+sums[2*x+2];
}
void build(vector<int> &a){
build(a,0,0,size);
}
void init(int n){
size=1;
while(size<n){
size*=2;
}
sums.assign(2*size,0LL);
}
ll sum(int l,int r,int x,int lx,int rx){
if(lx>=r || rx<=l){
return 0;
}
if(lx>=l && rx<=r){
return sums[x];
}
ll s1,s2;
int m=(lx+rx)/2;
s1=sum(l,r,2*x+1,lx,m);
s2=sum(l,r,2*x+2,m,rx);
return s1+s2;
}
ll sum(int l,int r){
return sum(l,r,0,0,size);
}
void set(int i,int v,int x,int lx,int rx){
if(rx-lx==1){
sums[x]=v;
return;
}
int m=(lx+rx)/2;
if(i<m){
set(i,v,2*x+1,lx,m);
}
else{
set(i,v,2*x+2,m,rx);
}
sums[x]=sums[2*x+1]+sums[2*x+2];
}
void set(int i,int v){
set(i,v,0,0,size);
}
};
int sum[MAX];
void update(int x,int v){
while(x<=n){
sum[x]+=v;
x+=x&-x;
}
}
int query(int x){
ll res=0;
while(x>=1){
res+=sum[x];
x-=x&-x;
}
return res;
}
void solve(){
cin >> n;
inputar(a,n);
unordered_map<int,int,custom_hash> c;
for(int i=0;i<n;i++){
c[a[i]]++;
b[i]=c[a[i]];
}
c.clear();
for(int i=n-1;i>=0;i--){
c[a[i]]++;
d[i]=c[a[i]];
}
vector<pii> v1;
vector<pii> v2;
for(int i=0;i<n;i++){
v1.pb(mp(b[i],i+1));
}
for(int i=0;i<n;i++){
v2.pb(mp(d[i],i+1));
}
sortv(v1);
sortv(v2);
reverse(all(v1));
reverse(all(v2));
int j=0;
ll res=0;
for(int i=0;i<n;i++){
while(j<n && v2[j].ff>=v1[i].ff){
update(v2[j].ss,-1);
j++;
}
int sum=query(n)-query(v1[i].ss);
res+=n-v1[i].ss+sum;
}
cout << res << "\n";
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
//USACO("angry");
//freopen("input.txt","r",stdin);
//cin >> t;
ll cnt1=1;
while(t--){
solve();
cnt1++;
}
}
/*
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
ifstream fin("template.in");
ofstream fout("template.out");
*/
/*
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
ifstream fin("template.in");
ofstream fout("template.out");
*/
/*
ll b[51][51];
b[0][0] = 1;
for (int n = 1; n <= 50; ++n){
b[n][0] = b[n][n] = 1;
for (int k = 1; k < n; ++k)
b[n][k] = b[n - 1][k - 1] + b[n - 1][k];
}
*/
20. Valid Parentheses | 746. Min Cost Climbing Stairs |
392. Is Subsequence | 70. Climbing Stairs |
53. Maximum Subarray | 1527A. And Then There Were K |
1689. Partitioning Into Minimum Number Of Deci-Binary Numbers | 318. Maximum Product of Word Lengths |
448. Find All Numbers Disappeared in an Array | 1155. Number of Dice Rolls With Target Sum |
415. Add Strings | 22. Generate Parentheses |
13. Roman to Integer | 2. Add Two Numbers |
515. Find Largest Value in Each Tree Row | 345. Reverse Vowels of a String |
628. Maximum Product of Three Numbers | 1526A - Mean Inequality |
1526B - I Hate 1111 | 1881. Maximum Value after Insertion |
237. Delete Node in a Linked List | 27. Remove Element |
39. Combination Sum | 378. Kth Smallest Element in a Sorted Matrix |
162. Find Peak Element | 1529A - Eshag Loves Big Arrays |
19. Remove Nth Node From End of List | 925. Long Pressed Name |
1051. Height Checker | 695. Max Area of Island |